package org.pps.gl.client.vue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.LoginInfo;
import org.pps.gl.client.data.UserInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RDC_Suggestion extends Composite {
	VerticalPanel suggestion;

	public RDC_Suggestion() {
		suggestion = new VerticalPanel();
		suggestion.setStyleName("profilMarge");
		Label titre = new Label("Vous connaissez peut-être :");
		titre.setStylePrimaryName("titre");
		suggestion.add(titre);
		
		// suggestion d'ajout
		GetInfoServiceAsync gis = GWT.create(GetInfoService.class);
		gis.doSuggestion(LoginInfo.getCurrentUser().getEmailAddress(), new AsyncCallback<HashMap<UserInfo, Integer>>() {
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(HashMap<UserInfo, Integer> result) {
				final List<Entry<UserInfo, Integer>> entries = new ArrayList<Entry<UserInfo, Integer>>(
						result.entrySet());
				for (Entry<UserInfo, Integer> u : entries) {
					System.out.println("SUCCESS !");
					HorizontalPanel line = new HorizontalPanel();
					line.setSpacing(5);
					// lien vers le profil
					Hyperlink hyperlink = new Hyperlink("",
							"user/" + u.getKey().getEmailAddress());
					hyperlink.setStyleName("ficheHyper");
					hyperlink.setStylePrimaryName("ficheHyper");
					line.add(hyperlink);
					hyperlink.setSize("33px", "30px");
					// nom de la personne
					Label label = new Label(u.getKey().getFirstname()
							+ " " + u.getKey().getLastname() + " ");
					label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
					line.add(label);
					line.setCellHeight(label, "10px");
					Label distance = new Label("");
					if (u.getValue() == 0)
						distance.setText("moi");
					if (u.getValue() == 1)
						distance.setText("contact");
					if ((u.getValue() > 1) && (u.getValue() < 9)) {
						distance.setText(u.getValue()
								.toString() + "ème niveau");
					}
					distance.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
					line.add(distance);
					line.setCellHeight(distance, "10px");
					line.setCellVerticalAlignment(
							distance,
							HasVerticalAlignment.ALIGN_MIDDLE);
					distance.setSize("100px", "20px");
					line.setCellVerticalAlignment(label,
							HasVerticalAlignment.ALIGN_MIDDLE);
					label.setSize("200px", "20px");
					suggestion.add(line);
					line.setSize("198px", "27px");
				}
			}
		});
		
		initWidget(suggestion);
	}

}
